﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    table=ST_PATENT;
    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QTableView::NoEditTriggers);

    if(QFile::exists(configFile))
    {
        QSettings config(configFile,QSettings::IniFormat);
        patLocation=config.value("Library/location").toString();
        if(patLocation.isEmpty())
            patLocation=FHS_SHARE;
    }
    else
        patLocation=FHS_SHARE;

    updateSearchBox();
    updateView();
    createMenu();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::updateView()
{
    model->setTable(table);
    model->select();
    setHeaderData();
}

void Widget::generateMenu()
{
    menu->addAction(QStringLiteral("新记录..."),this,&Widget::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新视图"),this,&Widget::updateView);
    menu->addAction(QStringLiteral("设置本地库"),this,&Widget::do_putLocation);
    menu->addSeparator();
    menu->addAction(QStringLiteral("进入编辑模式"),this,&Widget::edit_on);
}

void Widget::newItem()
{
    AddItem itemAdd;
    itemAdd.move(x() + 100,y() + 50);
    if(itemAdd.exec() == QDialog::Accepted)
        updateView();
}

void Widget::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(index.isValid())
        model->removeRow(index.row());
}

void Widget::edit_off()
{
    isEdit=false;
    ui->tableView->setEditTriggers(QTableView::NoEditTriggers);
    updateMenu();
}

void Widget::edit_on()
{
    isEdit=true;
    ui->tableView->setEditTriggers(QTableView::DoubleClicked);
    updateMenu();
}

void Widget::do_putLocation()
{
    QString location=QFileDialog::getExistingDirectory(this,
                                                       QStringLiteral("选择本地文件夹"),
                                                       Location::document);
    if(!location.isEmpty())
    {
        putLocation(location);
        patLocation=location;
    }
}

void Widget::putLocation(const QString &location)
{
    QSettings config(configFile,QSettings::IniFormat);
    config.setValue("Library/location",location);
}

void Widget::updateSearchBox()
{
    ui->searchBox->clear();
    ui->searchBox->addItem(QStringLiteral("本地搜索"));

    if(!items.isEmpty())
        items.clear();

    QSqlQuery query;
    query.exec(QString("select label,detail from %1 where topic='%2'")
               .arg(ST_PREFIX)
               .arg(QStringLiteral("专利检索")));
    while (query.next()) {
        QString label=query.value("label").toString();
        QString detail=query.value("detail").toString();
        items.insert(label,detail);
    }

    if(!items.isEmpty())
        ui->searchBox->addItems(items.keys());
    ui->searchBox->setCurrentIndex(0);
}

void Widget::setHeaderData()
{
    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("公开号"));
    ui->tableView->setColumnWidth(0,160);
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("公开日期"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("申请人"));
    ui->tableView->setColumnWidth(2,240);
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("标题"));
}

void Widget::on_keyword_returnPressed()
{
    QString kw=ui->keyword->text().trimmed();
    if(kw.isEmpty())
        return;

    if(ui->searchBox->currentIndex() == 0)
    {
        model->setTable(table);
        model->setFilter(QString("pub_id = '%1' OR label like '%%1%'")
                         .arg(kw));
        model->select();
        setHeaderData();
    }
    else
    {
        QString key=ui->searchBox->currentText();
        QString prefix=items.value(key);
        QString url=prefix + kw;
        QDesktopServices::openUrl(QUrl(url));
    }
}

void Widget::on_tableView_doubleClicked(const QModelIndex &index)
{
    if(!isEdit)
    {
        QString pub_id=index.sibling(index.row(),0).data().toString();
        QString file=patLocation + QString("/") + pub_id + ".pdf";
        if(QFile::exists(file))
            QDesktopServices::openUrl(QUrl::fromLocalFile(file));
        else
        {
            QString url=PAT_SEARCH + pub_id;
            QDesktopServices::openUrl(QUrl(url));
        }
    }
}
